package cn.com.yx.ms.service.impl;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.dbmodel.DevEquStand;
import cn.com.yx.ms.domain.dbmodel.DevEquStore;
import cn.com.yx.ms.domain.dbmodel.LineStandInfo;
import cn.com.yx.ms.domain.dbmodel.LinestandMap;
import cn.com.yx.ms.domain.line.DevStandModel;
import cn.com.yx.ms.persistence.DevEquStandMapper;
import cn.com.yx.ms.persistence.DevEquStoreMapper;
import cn.com.yx.ms.persistence.LineStandInfoMapper;
import cn.com.yx.ms.persistence.LineStandMapMapper;
import cn.com.yx.ms.service.IStandService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * ApphubServiceImpl
 */
@Service
@Transactional
public class StandServiceImpl implements IStandService {

    @Resource
    private DevEquStoreMapper devEquStoreMapper;
    @Resource
    private DevEquStandMapper devEquStandMapper;
    @Resource
    private LineStandInfoMapper lineStandInfoMapper;
    @Resource
    private LineStandMapMapper lineStandMapMapper;

    @Override
    public DevStandModel findOne(String equipmentguid) {
        DevStandModel devStandModel = null;
        DevEquStand devEquStand = devEquStandMapper.selectByPrimaryKey(equipmentguid);
        if (devEquStand != null) {
            LineStandInfo lineStandInfo = lineStandInfoMapper.selectByPrimaryKey(devEquStand.getSguid());
            devStandModel = new DevStandModel();
            devStandModel.setEquipmentguid(equipmentguid);
            devStandModel.setDevsn(devEquStand.getEquipmentno());
            devStandModel.setSguid(devEquStand.getSguid());
            devStandModel.setSname(lineStandInfo.getSname());
            devStandModel.setSlat(lineStandInfo.getSlat().toString()); //纬度
            devStandModel.setSlon(lineStandInfo.getSlon().toString()); //经度
            devStandModel.setSdirect(lineStandInfo.getSdirect()); //站台方位
        }
        return devStandModel;
    }

    @Override
    public ApiResult save(DevStandModel devStandModel) {
        ApiResult result = null;

        if (!StringUtils.isNoneBlank(devStandModel.getEquipmentguid())) {
            result = new ApiResult(AppConstant.MSG_FAILURE, false, "请选中设备");
            return result;
        }

        if (!StringUtils.isNoneBlank(devStandModel.getSguid())) {
            result = new ApiResult(AppConstant.MSG_FAILURE, false, "请选中站台");
            return result;
        }

//        if (devEquStandMapper.exist(devStandModel.getEquipmentguid()) > 0) {
//            result = new ApiResult(AppConstant.MSG_FAILURE, false, "该设备与站台的关联已经存在");
//            return result;
//        }

        DevEquStore old = devEquStoreMapper.selectByPrimaryKey(devStandModel.getEquipmentguid());
        int count = devEquStandMapper.deleteByPrimaryKey(devStandModel.getEquipmentguid());
        DevEquStand devEquStand = new DevEquStand();
        devEquStand.setEquipmentguid(old.getEquipmentguid());
        devEquStand.setEquipmentno(old.getEquipmentno());
        devEquStand.setSguid(devStandModel.getSguid());
        count += devEquStandMapper.insert(devEquStand);
        if (count > 0) {
            result = new ApiResult(AppConstant.MSG_SUCCESS, true, "绑定成功");
        } else {
            result = new ApiResult(AppConstant.MSG_SUCCESS, true, "绑定失败");
        }
        return result;
    }

    @Override
    public int insert(DevEquStand devEquStand) {
        return devEquStandMapper.insert(devEquStand);
    }

    @Override
    public int delete(String equipmentguid) {
        return devEquStandMapper.deleteByPrimaryKey(equipmentguid);
    }

    @Override
    public int exist(String equipmentguid) {
        return devEquStandMapper.exist(equipmentguid);
    }

    @Override
    public List<DevStandModel> find(ApiQueryBase apiQueryBase) {
        return devEquStandMapper.find(apiQueryBase);
    }

    @Override
    public List<LineStandInfo> findAllStandInfo(ApiQueryBase apiQueryBase) {
        return lineStandInfoMapper.find(apiQueryBase);
    }

    @Override
    public ApiResult findLineinfor(ApiQueryBase query) {
        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        //List<LinestandMap> linestandMaps=lineStandMapMapper.findByconditions(query);
       // System.out.println("长度"+linestandMaps.size());
        Page<LinestandMap> page = (Page<LinestandMap>) lineStandMapMapper.findByconditions(query);
        ApiPager<LinestandMap> apiPager = new ApiPager<LinestandMap>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);
    }
}